package com.cencat.route.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cencat.route.entity.Driver;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 司机信息数据访问层
 * 提供司机信息的数据库操作接口
 * 
 * @author cencat
 * @version 1.0
 * @since 2024-01-01
 */
@Mapper
public interface DriverMapper extends BaseMapper<Driver> {

    /**
     * 分页查询司机信息
     */
    Page<Driver> selectDriverPage(Page<Driver> page, @Param("name") String name, 
                                  @Param("phone") String phone, @Param("status") String status);

    /**
     * 条件查询司机信息
     */
    List<Driver> searchDrivers(@Param("conditions") Map<String, Object> conditions);

    /**
     * 根据手机号查询司机
     */
    @Select("SELECT * FROM driver WHERE phone = #{phone} AND deleted = 0")
    Driver selectByPhone(@Param("phone") String phone);

    /**
     * 根据身份证号查询司机
     */
    @Select("SELECT * FROM driver WHERE id_card = #{idCard} AND deleted = 0")
    Driver selectByIdCard(@Param("idCard") String idCard);

    /**
     * 根据驾驶证号查询司机
     */
    @Select("SELECT * FROM driver WHERE license_number = #{licenseNumber} AND deleted = 0")
    Driver selectByLicenseNumber(@Param("licenseNumber") String licenseNumber);

    /**
     * 根据状态查询司机列表
     */
    @Select("SELECT * FROM driver WHERE status = #{status} AND deleted = 0")
    List<Driver> selectByStatus(@Param("status") String status);

    /**
     * 根据车辆类型查询司机列表
     */
    @Select("SELECT * FROM driver WHERE vehicle_type = #{vehicleType} AND deleted = 0")
    List<Driver> selectByVehicleType(@Param("vehicleType") String vehicleType);

    /**
     * 批量更新司机状态
     */
    @Update("<script>" +
            "UPDATE driver SET status = #{status}, updated_time = NOW() " +
            "WHERE id IN " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int batchUpdateStatus(@Param("ids") List<Long> ids, @Param("status") String status);

    /**
     * 批量更新司机信用分数
     */
    @Update("<script>" +
            "UPDATE driver SET credit_score = #{creditScore}, updated_time = NOW() " +
            "WHERE id IN " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int batchUpdateCreditScore(@Param("ids") List<Long> ids, @Param("creditScore") BigDecimal creditScore);

    /**
     * 批量更新司机位置信息
     */
    @Update("<script>" +
            "UPDATE driver SET current_latitude = #{latitude}, current_longitude = #{longitude}, updated_time = NOW() " +
            "WHERE id IN " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int batchUpdateLocation(@Param("ids") List<Long> ids, @Param("latitude") BigDecimal latitude, 
                           @Param("longitude") BigDecimal longitude);

    /**
     * 获取司机统计分析
     */
    Map<String, Object> getDriverStatistics(@Param("startTime") LocalDateTime startTime, 
                                           @Param("endTime") LocalDateTime endTime);

    /**
     * 智能匹配司机
     */
    List<Driver> intelligentMatchDrivers(@Param("conditions") Map<String, Object> conditions);

    /**
     * 查询附近司机
     */
    List<Driver> findNearbyDrivers(@Param("latitude") BigDecimal latitude, 
                                  @Param("longitude") BigDecimal longitude, 
                                  @Param("radius") BigDecimal radius);

    /**
     * 获取在线司机列表
     */
    @Select("SELECT * FROM driver WHERE status = 'ONLINE' AND deleted = 0")
    List<Driver> selectOnlineDrivers();

    /**
     * 获取空闲司机列表
     */
    @Select("SELECT * FROM driver WHERE status = 'FREE' AND deleted = 0")
    List<Driver> selectFreeDrivers();

    /**
     * 获取忙碌司机列表
     */
    @Select("SELECT * FROM driver WHERE status = 'BUSY' AND deleted = 0")
    List<Driver> selectBusyDrivers();

    /**
     * 检查司机是否存在
     */
    @Select("SELECT COUNT(*) > 0 FROM driver WHERE phone = #{phone} AND deleted = 0")
    boolean checkDriverExists(@Param("phone") String phone);

    /**
     * 司机注册
     */
    int registerDriver(@Param("driver") Driver driver);

    /**
     * 司机认证
     */
    @Update("UPDATE driver SET verified = 1, verified_time = NOW(), updated_time = NOW() WHERE id = #{id}")
    int verifyDriver(@Param("id") Long id);

    /**
     * 司机激活
     */
    @Update("UPDATE driver SET status = 'ACTIVE', updated_time = NOW() WHERE id = #{id}")
    int activateDriver(@Param("id") Long id);

    /**
     * 司机停用
     */
    @Update("UPDATE driver SET status = 'INACTIVE', updated_time = NOW() WHERE id = #{id}")
    int deactivateDriver(@Param("id") Long id);

    /**
     * 更新司机最后活跃时间
     */
    @Update("UPDATE driver SET last_active_time = NOW(), updated_time = NOW() WHERE id = #{id}")
    int updateLastActiveTime(@Param("id") Long id);

    /**
     * 获取司机评分统计
     */
    Map<String, Object> getDriverRatingStatistics(@Param("driverId") Long driverId);

    /**
     * 获取司机订单统计
     */
    Map<String, Object> getDriverOrderStatistics(@Param("driverId") Long driverId, 
                                                @Param("startTime") LocalDateTime startTime, 
                                                @Param("endTime") LocalDateTime endTime);

    /**
     * 获取司机收入统计
     */
    Map<String, Object> getDriverIncomeStatistics(@Param("driverId") Long driverId, 
                                                 @Param("startTime") LocalDateTime startTime, 
                                                 @Param("endTime") LocalDateTime endTime);
}